package com.zzq.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzq.domain.entity.SmileCheckRecord;
import com.zzq.enumerate.CheckDayStatusEnum;
import com.zzq.service.SmileCheckRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CheckTaskService {

    @Autowired
    SmileCheckRecordService checkRecordService;

    @Scheduled(cron = "0 0 0 ? * *")
    public void CheckDayTask() {
        log.info("更新签到信息开始");
        LambdaQueryWrapper<SmileCheckRecord> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SmileCheckRecord::getCheckDayStatus, CheckDayStatusEnum.TODAY.getName()).or()
                .eq(SmileCheckRecord::getCheckDayStatus, CheckDayStatusEnum.YESTERDAY.getName());
        List<SmileCheckRecord> checkRecordList = checkRecordService.list(queryWrapper);
        checkRecordList.stream().forEach(smileCheckRecord -> {
            if (CheckDayStatusEnum.TODAY.getName().equals(smileCheckRecord.getCheckDayStatus())) {
                smileCheckRecord.setCheckDayStatus(CheckDayStatusEnum.YESTERDAY.getName());
                checkRecordService.updateById(smileCheckRecord);
            } else if (CheckDayStatusEnum.YESTERDAY.getName().equals(smileCheckRecord.getCheckDayStatus())) {
                smileCheckRecord.setCheckDayStatus(CheckDayStatusEnum.PASTDAY.getName());
                checkRecordService.updateById(smileCheckRecord);
            }
            log.info("正在更新签到：用户{}",smileCheckRecord.getClientNo());
        });

        log.info("更新签到信息结束");

    }
}
